//
// Created by leoking870 on 2017/9/29.
// Copyright (c) 2017 leoking870. All rights reserved.
//

#import "YKClientDataDetailController.h"
#import "YKInputView.h"
#import "YKSelectView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKInsetTextField.h"
#import "YKInputMultilineView.h"
#import "YKClientDataDetailEditViewController.h"
#import "YKViewMaintainLogViewController.h"
#import "YKViewVisitLogViewController.h"
#import "YKCustomerCategoryModel.h"
#import "YKCustomerRegionModel.h"
#import "YKMedQuaModel.h"
#import "YKSettlemenTypeModel.h"
#import "YKClientDataDetail.h"
@interface YKClientDataDetailController ()<YKClientDataDetailEditViewControllerDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YKClientDataDetail *mYKClientDataDetail;
@end

@implementation YKClientDataDetailController {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查看客户详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-44);
    }];
    
    UIButton* viewMaintainLogButton = [[UIButton alloc]init];
    [self.view addSubview:viewMaintainLogButton];
    UIButton *viewVisitLogButton = [[UIButton alloc]init];
    [self.view addSubview:viewVisitLogButton];
    UIButton *maintainClientDataButton = [[UIButton alloc]init];
    [self.view addSubview:maintainClientDataButton];
    [viewMaintainLogButton setTitle:@"查看维护日志" forState:UIControlStateNormal];
    [viewVisitLogButton setTitle:@"查看拜访日志" forState:UIControlStateNormal];
    [maintainClientDataButton setTitle:@"维护客户资料" forState:UIControlStateNormal];
    [viewMaintainLogButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [viewVisitLogButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [maintainClientDataButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [viewMaintainLogButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [viewVisitLogButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [maintainClientDataButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    viewMaintainLogButton.titleLabel.font = [UIFont systemFontOfSize:14];
    viewVisitLogButton.titleLabel.font = [UIFont systemFontOfSize:14];
    maintainClientDataButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [maintainClientDataButton addTarget:self action:@selector(maintainClientData:) forControlEvents:UIControlEventTouchUpInside];
    [viewMaintainLogButton addTarget:self action:@selector(viewMaintainLog:) forControlEvents:UIControlEventTouchUpInside];
    [viewVisitLogButton addTarget:self action:@selector(viewVisitLog:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[viewMaintainLogButton,viewVisitLogButton,maintainClientDataButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[viewMaintainLogButton,viewVisitLogButton,maintainClientDataButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"customerDataManager/customerDataManagerDetail.htm" parameters:@{@"id":self.mYKClientDataModel.ID} success:^(id responseObject) {
        [self tf_showStateView:kTFViewStateNone];
        [CommonDataManager sharedInstance].medQuaList = [NSArray yy_modelArrayWithClass:[YKMedQuaModel class] json:responseObject[@"medQuaList"]];
        [CommonDataManager sharedInstance].settlemenTypeList = [NSArray yy_modelArrayWithClass:[YKSettlemenTypeModel class] json:responseObject[@"settlemenTypeList"]];
        [CommonDataManager sharedInstance].customerTypeList = [NSArray yy_modelArrayWithClass:[YKTypeModel class] json:responseObject[@"customerTypeList"]];
        self.mYKClientDataDetail = [YKClientDataDetail yy_modelWithJSON:responseObject[@"customerDataManagerMap"]];
        [self updateViews];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"customerDataManager/customerDataManagerDetail.htm" parameters:@{@"id":self.mYKClientDataModel.ID} success:^(id responseObject) {
        [self tf_showStateView:kTFViewStateNone];
//        [CommonDataManager sharedInstance].categorys = [responseObject valueForKey:@"categorys"];
//        [CommonDataManager sharedInstance].regions = [responseObject valueForKey:@"regions"];
        [CommonDataManager sharedInstance].medQuaList = [NSArray yy_modelArrayWithClass:[YKMedQuaModel class] json:responseObject[@"medQuaList"]];
        [CommonDataManager sharedInstance].settlemenTypeList = [NSArray yy_modelArrayWithClass:[YKSettlemenTypeModel class] json:responseObject[@"settlemenTypeList"]];
        
        self.mYKClientDataDetail = [YKClientDataDetail yy_modelWithJSON:responseObject[@"customerDataManagerMap"]];
        
        [self updateViews];
        
        
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}


- (void)updateViews {
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[YKInputView class]]) {
            [obj removeFromSuperview];
        }
        else if ([obj isKindOfClass:[YKInputMultilineView class]]){
            [obj removeFromSuperview];
        }
        else if ([obj isKindOfClass:[YKSelectView class]]){
            [obj removeFromSuperview];
        }
        else if ([obj isKindOfClass:[YKMultiSelectDropdownView class]]){
            [obj removeFromSuperview];
        }
    }];
    
    
    YKInputView* drugCodeView = [[YKInputView alloc]initWithTitle:@"客户编码" input:self.mYKClientDataModel.C_CODE];
    
    [self.scrollView addSubview:drugCodeView];
    
    YKInputView* clientNameView = [[YKInputView alloc]initWithTitle:@"客户名称" input:self.mYKClientDataDetail.C_NAME];
    [self.scrollView addSubview:clientNameView];
    
    
    NSArray *data = @[
                      @[@"医疗器械",@"医疗器械1",@"医疗器械2",@"医疗器械3"],
                      @[@"医疗器械",@"医疗器械1",@"医疗器械2",@"医疗器械3"]
                      ];
    
    YKTypeModel *typeModels = [[[CommonDataManager sharedInstance].customerTypeList tf_filteredWithPredicateBlock:^BOOL(YKTypeModel *  _Nonnull obj, NSInteger idx) {
        return [self.mYKClientDataDetail.CUSTOMERTYPE isEqualToString:obj.id_p] || [self.mYKClientDataDetail.CUSTOMERTYPE isEqualToString:obj.code];
    }]firstObject];
    YKSelectView* categoryView = [[YKSelectView alloc] initWithTitle:@"客户分类" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].customerTypeList initialText:typeModels];
    [self.scrollView addSubview:categoryView];
    
    YKSettlemenTypeModel *settlemenTypeModel = [[[CommonDataManager sharedInstance].settlemenTypeList tf_filteredWithPredicateBlock:^BOOL(YKSettlemenTypeModel *  _Nonnull obj, NSInteger idx) {
        return [self.mYKClientDataDetail.SETTLEMENTTYPE isEqualToString:obj.id_p] || [self.mYKClientDataDetail.SETTLEMENTTYPE isEqualToString:obj.code];
    }]firstObject];
    YKSelectView* typeView = [[YKSelectView alloc] initWithTitle:@"结算类型" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].settlemenTypeList initialText:settlemenTypeModel];
    [self.scrollView addSubview:typeView];
    
    YKMedQuaModel *selectedMedQuaModel = [[[CommonDataManager sharedInstance].medQuaList tf_filteredWithPredicateBlock:^BOOL(YKMedQuaModel*  _Nonnull obj, NSInteger idx) {
        return [obj.id_p isEqualToString:self.mYKClientDataDetail.MEDICAL_QUALIFICATION] || [obj.code isEqualToString:self.mYKClientDataDetail.MEDICAL_QUALIFICATION];
    }]firstObject];
    
    YKSelectView* insuranceView = [[YKSelectView alloc] initWithTitle:@"医保资质" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].medQuaList initialText:selectedMedQuaModel];
    self.mYKClientDataDetail.MEDICAL_NAME = selectedMedQuaModel.MQ_NAME;
    [self.scrollView addSubview:insuranceView];
//
//    YKMultiSelectDropdownView *multiSelectView1 = [[YKMultiSelectDropdownView alloc]initWithTitle:@"区域定位"
//                                                                                             rows:@[@[@"江苏省"],@[@"南京市"]]];
//    [self.scrollView addSubview:multiSelectView1];
    
    
    
    [self.scrollView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.scrollView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
    

    
    YKInputView* yinshouMoneyView = [[YKInputView alloc]initWithTitle:@"累积订单" input:self.mYKClientDataDetail.LJDD_SUM];
    [self.scrollView addSubview:yinshouMoneyView];
    
    YKInputView* yishouMoneyView = [[YKInputView alloc]initWithTitle:@"累积收款" input:self.mYKClientDataDetail.LJSK_SUM];
    [self.scrollView addSubview:yishouMoneyView];


    YKInputView* contactView = [[YKInputView alloc]initWithTitle:@"联系人" input:self.mYKClientDataDetail.CONSIGNEE];
    [self.scrollView addSubview:contactView];
    YKInputView* phoneNumberView = [[YKInputView alloc]initWithTitle:@"手机号码" input:self.mYKClientDataDetail.CONSIGNEE_PHONE];
    [self.scrollView addSubview:phoneNumberView];

    YKInputView* detailAddressTF = [[YKInputView alloc]initWithTitle:@"详细地址" input:self.mYKClientDataDetail.RECEIVING_DETAILADDRESS];
    [self.scrollView addSubview:detailAddressTF];
    
//
//    YKInsetTextField *detailAddressTF = [[YKInsetTextField alloc]init];
//    detailAddressTF.placeholder = @"请输入详细地址";
//    detailAddressTF.text = self.mYKClientDataDetail.RECEIVING_DETAILADDRESS;
//    [self.scrollView addSubview:detailAddressTF];
//    [detailAddressTF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(30);
//        make.leading.equalTo(self.view.mas_leading).offset(13);
//        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
//    }];
//    detailAddressTF.contentInsets = UIEdgeInsetsMake(0, 8, 0, 0);
//    detailAddressTF.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];

    
    YKInputView* registerMoneyView = [[YKInputView alloc]initWithTitle:@"注册资金" input:self.mYKClientDataDetail.REG_CAPITAL];
    [self.scrollView addSubview:registerMoneyView];
    
    YKInputView* foundDateView = [[YKInputView alloc]initWithTitle:@"成立时间" input:self.mYKClientDataDetail.ESTABLISH];
    [self.scrollView addSubview:foundDateView];
    
    YKInputView* doctorCountView = [[YKInputView alloc]initWithTitle:@"医生数量" input:self.mYKClientDataDetail.DOCTORCOUNT];
    [self.scrollView addSubview:doctorCountView];
    
    YKInputView* nerseCountView = [[YKInputView alloc]initWithTitle:@"护工数量" input:self.mYKClientDataDetail.NURSECOUNT];
    [self.scrollView addSubview:nerseCountView];
    
    
    
    
    
    YKInputMultilineView *currentChannelView = [[YKInputMultilineView alloc]initWithTitle:@"现有渠道" input:self.mYKClientDataDetail.PURCHASING_CHANNEL];
    [self.scrollView addSubview:currentChannelView];
    
    
    YKInputMultilineView *saleDrugsView = [[YKInputMultilineView alloc]initWithTitle:@"在售主流药品" input:self.mYKClientDataDetail.MAINSTREAM_DRUGS];
    [self.scrollView addSubview:saleDrugsView];
    
    YKInputMultilineView *yearBuyView = [[YKInputMultilineView alloc]initWithTitle:@"年采购情况" input:self.mYKClientDataDetail.PURCHASING_SITUATION];
    [self.scrollView addSubview:yearBuyView];
    
    YKInputMultilineView *yearSaleView = [[YKInputMultilineView alloc]initWithTitle:@"年销售情况" input:self.mYKClientDataDetail.SALES_SITUATION];
    [self.scrollView addSubview:yearSaleView];
    
    [@[yinshouMoneyView, yishouMoneyView,contactView, phoneNumberView, detailAddressTF,registerMoneyView,foundDateView,doctorCountView,nerseCountView,categoryView,typeView,insuranceView,currentChannelView,saleDrugsView,yearBuyView,yearSaleView] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.scrollView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
    
    [self.scrollView.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@0 leadSpace:8 tailSpace:8];
    
//
//    multiSelectView.modelData = (NSArray <YKDropdown> *) [CommonDataManager sharedInstance].customerCategoryList;
//    multiSelectView.selectedCodes = [self.mYKClientDataDetail.CATEGORY componentsSeparatedByString:@"|"];
////
//    multiSelectView1.modelData = [CommonDataManager sharedInstance].customerRegionList;
//    multiSelectView1.selectedCodes = [self.mYKClientDataDetail.REGION componentsSeparatedByString:@"|"];
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[YKInputView class]]) {
            YKInputView *inputView = obj;
            inputView.editable = NO;
        }
        else if ([obj isKindOfClass:[YKInputMultilineView class]]){
            YKInputMultilineView *multiInputView = obj;
            multiInputView.editable = NO;
        }
        else if ([obj isKindOfClass:[YKSelectView class]]){
            YKSelectView *selectView = obj;
            selectView.enabled = NO;
        }
        else if ([obj isKindOfClass:[YKMultiSelectDropdownView class]]){
            YKMultiSelectDropdownView *selectView = obj;
            selectView.enabled = NO;
        }
        else if ([obj isKindOfClass:[YKInsetTextField class]]){
            YKInsetTextField *text = obj;
            text.enabled = NO;
        }
    }];
}
- (void)maintainClientData:(id)sender {
    YKClientDataDetailEditViewController *vc = [[YKClientDataDetailEditViewController alloc]init];
    vc.mYKClientDataDetail = self.mYKClientDataDetail;
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clientDataDetailEditSuccess {
    [self loadData];
}

- (void)viewMaintainLog:(id)sender {
    YKViewMaintainLogViewController *vc = [[YKViewMaintainLogViewController alloc]init];
    vc.mYKClientDataDetail = self.mYKClientDataDetail;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewVisitLog:(id)sender {
    YKViewVisitLogViewController *vc = [[YKViewVisitLogViewController alloc]init];
    vc.mYKClientDataDetail = self.mYKClientDataDetail;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
